package com.ycy.skip;

import com.ycy.BaseTest;
import org.flowable.bpmn.model.MultiInstanceLoopCharacteristics;
import org.flowable.bpmn.model.UserTask;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.Test;

import java.util.*;

public class skipExpression2 extends BaseTest {

  @Test
  public void creatProcDef() {
    // 部署流程
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deployment = repositoryService.createDeployment()
            .addString("rdfa-bp-version","1111")
            .addClasspathResource("skip/skipExpression.bpmn20.xml")
            .deploy();
    System.out.println(deployment);
  }

  @Test
  public void createProcInst() {
    RuntimeService runtimeService = processEngine.getRuntimeService();
    HashMap<String, Object> variables = new HashMap<>();
    List<Long> zhixingrens = new ArrayList<>();
    zhixingrens.add(110L);
    zhixingrens.add(112L);
    variables.put("ASSIGNEE_zhixingren1_LIST", zhixingrens);

    variables.put("_FLOWABLE_SKIP_EXPRESSION_ENABLED", true);

    //添加审批过的
    Set<Long> ignores1 = new HashSet<>();
    ignores1.add(110L);
    ignores1.add(112L);
    variables.put("RDFA_passed_assignee_List1", ignores1);

    Set<String> ignores2 = new HashSet<>();
//    ignores2.add("ycy");
    variables.put("RDFA_passed_assignee_List2", ignores2);

    List<String> zhixingrens2 = new ArrayList<>();
    zhixingrens2.add("ycy");
    zhixingrens2.add("xdm");
    variables.put("ASSIGNEE_zhixingren2_LIST", zhixingrens2);

    ProcessInstance processInstance =
            runtimeService.startProcessInstanceByKey("skiping", variables);
    System.out.println(processInstance.getProcessInstanceId());
  }

  @Test
  public void pass() {
    taskService.complete("242531");
  }

  @Test
  public void addAssignee() {

    //加签 测试 也能自动跳过
    Map<String, Object> executionVariables = new HashMap<>();
    executionVariables.put("assignee", "xdm");
    executionVariables.put("rdfa_task_operate_type", "addassignee");

    //设置 不自动跳过
    executionVariables.put("_FLOWABLE_SKIP_EXPRESSION_ENABLED", true);

    Execution b = runtimeService.addMultiInstanceExecution("B", "15001", executionVariables);
    System.out.println(b);

  }

  @Test
  public void chaneAssignee() {

    //加签 测试 也能自动跳过
    Map<String, Object> executionVariables = new HashMap<>();
    executionVariables.put("assignee", "xdm");
    executionVariables.put("rdfa_task_operate_type", "addassignee");

    //设置 不自动跳过
    executionVariables.put("_FLOWABLE_SKIP_EXPRESSION_ENABLED", true);

    Execution b = runtimeService.addMultiInstanceExecution("B", "15001", executionVariables);
//    runtimeService.createChangeActivityStateBuilder()
    System.out.println(b);

  }

  @Test
  public void cancelTask() {
    runtimeService.deleteProcessInstance("5004", "不能没有删除理由");
  }

  @Test
  public void queryTask() {
    //在完成的任务历史中，查询真正执行过的审批人
    List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().processInstanceId("152501").
            orderByHistoricTaskInstanceStartTime().desc().list();
    for (HistoricTaskInstance historicTaskInstance : list) {
      if (historicTaskInstance.getAssignee() != null && historicTaskInstance.getDeleteReason() == null) {
        System.out.println(historicTaskInstance.getDeleteReason() + " " + historicTaskInstance.getAssignee());
        String taskDefinitionKey = historicTaskInstance.getTaskDefinitionKey();
      }
    }
  }


}
